package com.ruoyi;

import com.ruoyi.stock2.domain.STOCK;
import com.ruoyi.stock2.domain.TRADEDATA;
import com.ruoyi.stock2.mapper.STOCKMapper;
import com.ruoyi.stock2.service.ITRADEDATAService;
import com.ruoyi.stock2.utils.DateUtils2;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.*;
import java.math.BigDecimal;
import java.util.*;
import org.junit.Test;

@RunWith(SpringRunner.class)
@SpringBootTest
public class seq2seqTrain {

	private static final Logger log = LoggerFactory.getLogger(seq2seqTrain.class);

	@Autowired(required = false)
	STOCKMapper stockMapper;

	@Autowired(required = false)
	ITRADEDATAService itradedataService;

	@Test
	public void productFastTrainTextintoSelectModelFromPca (){

		List<STOCK> stocks = stockMapper.selectSTOCKList(null);
		for(int i=0; i<stocks.size(); i++){
			File file = new File("E:\\seq2seq\\seq2seq"+stocks.get(i).getSymbol()+"train.txt");
			BufferedWriter writer = null;
			try {
				if (!file.exists()) {
					file.createNewFile();
				} else {
					file.delete();
					file.createNewFile();
				}
				writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "utf-8"));
				TRADEDATA tradedata = new TRADEDATA();
				tradedata.setSymbol(stocks.get(i).getSymbol());
				List<TRADEDATA> list = itradedataService.selectTRADEDATAList(tradedata);
				if(list.size()<3000){
					continue;
				}
				for(int j=0; j<list.size()-80; j++){
					StringBuilder stringBuilderIt1 = new StringBuilder();
					StringBuilder stringBuilderIt2 = new StringBuilder();
					stringBuilderIt1.append(list.get(j).getSymbol()).append(",");
					//stringBuilderIt1.append(DateUtils2.formatDateToString(list.get(j).getDatetime(),"yyyyMMdd")).append(",");

					if(list.get(j).getAverageprice5()==null){
						System.out.println(list.get(j).getDatetime());
						continue;
					}
					List<BigDecimal> lista = new ArrayList<BigDecimal>();
					for(int k=j; k<j+80; k++){
						//选出最大值
						lista.add(list.get(k).getCloseprice());
					}
					BigDecimal max = Collections.max(lista);
					System.out.println(max);

					int jishuqi = 0 ;
					for(int k=j; k<j+80; k++){
						TRADEDATA ojb = list.get(k);
						//stringBuilderIt.append(DateUtils2.formatDateToString(list.get(k).getDatetime(),"yyyyMMdd_")).append(ojb.getCloseprice()).append(" ");
						String zf = "u";
						if(ojb.getAverageprice5()!=null && ojb.getCloseprice().compareTo(ojb.getAverageprice5())<=0){
							zf = "d";
						}else{
							zf = "u";
						}
						jishuqi++;
						if(jishuqi==40){
							stringBuilderIt1.append(DateUtils2.formatDateToString(list.get(k).getDatetime(),"yyyyMMdd")).append(",");//加入最后的交易日期
							stringBuilderIt2.append(ojb.getCloseprice().divide(max, 2, BigDecimal.ROUND_HALF_UP)).append(zf).append(",");
						}else if(jishuqi>40){
							stringBuilderIt2.append(ojb.getCloseprice().divide(max, 2, BigDecimal.ROUND_HALF_UP)).append(" ");
						} else{
							stringBuilderIt2.append(ojb.getCloseprice().divide(max, 2, BigDecimal.ROUND_HALF_UP)).append(zf).append(" ");
						}
					}
					writer.write(stringBuilderIt1.append(stringBuilderIt2) + "\r\n");
				}

				writer.flush();

			} catch (IOException e1) {
				e1.printStackTrace();
			} finally {
				try {
					if (writer != null) {
						writer.close();
					}
				} catch (IOException  e) {
					e.printStackTrace();
				}
			}
		}
	}



	/**
	 * @param newStr 一行文本
	 * @param fileName 文件名（带文件路径）
	 * @throws IOException
	 */
	public void writeTxtFile(String newStr, String fileName) {
		byte[] sourceByte = new byte[0];
		try {
			sourceByte = newStr.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (null != sourceByte) {
			try {
				File file = new File(fileName); // 文件路径（路径+文件名）
				if (!file.exists()) { // 文件不存在则创建文件，先创建目录
					File dir = new File(file.getParent());
					dir.mkdirs();
					file.createNewFile();
				}
				FileOutputStream outStream = new FileOutputStream(file); // 文件输出流用于将数据写入文件
				outStream.write(sourceByte);
				outStream.close(); // 关闭文件输出流
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
